package com.zjut.zs.excel.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zjut.an.acquisitionhistory.dao.AnAcquisitionHistory;
import com.zjut.an.acquisitionhistory.manager.AnAcquisitionHistoryManager;
import com.zjut.an.admin.dao.AnAdmin;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.log.dao.AnLog;
import com.zjut.an.log.manager.AnLogManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.DateUtil;
import com.zjut.an.util.tools.MD5;
import com.zjut.an.util.tools.ZipUtil;
import com.zjut.common.idgen.IdGenerate;
import com.zjut.zs.confirm.dao.ZsConfirm;
import com.zjut.zs.excel.manager.ZsExcelManager;
import com.zjut.zs.school.dao.ZsSchool;
import com.zjut.zs.school.manager.ZsSchoolManager;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.user.manager.ZsUserManager;
import com.zjut.zs.userinfo.dao.ZsUserinfo;
import com.zjut.zs.userinfo.manager.ZsUserinfoManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.userstatus.manager.ZsUserStatusManager;

@SuppressWarnings("serial")
public class ZsExcelAction extends ActionSupport implements ServletResponseAware,
		ServletRequestAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private Map<String, Object> dataMap;
	private String selectCol;
	// 封装上传文件域的属性
	private File file;
	// 封装上传文件类型的属性
	private String fileContentType;
	// 封装上传文件名的属性
	private String fileFileName;
	// 接受依赖注入的属性
	private String savePath;

	@Resource(name = "ZsUserManager")
	private ZsUserManager manager;
	
	@Resource(name = "ZsExcelManager")
	private ZsExcelManager eManager;
	
	@Resource(name = "ZsUserinfoManager")
	private ZsUserinfoManager imanager;
	
	@Resource(name = "ZsUserStatusManager")
	private ZsUserStatusManager smanager;

	@Resource(name = "AnConfigManager")
	private AnConfigManager anConfigManager;

	@Resource(name = "ZsSchoolManager")
	private ZsSchoolManager scmanager;
	
	@Resource(name = "AnAcquisitionHistoryManager")
	private AnAcquisitionHistoryManager acmanager;

	@Resource(name = "AnLogManager")
	private AnLogManager logManager;
	
	@Resource(name = "IdGenerate")
	private IdGenerate idgenerate;

	@Resource(name = "MD5")
	private MD5 md5;

	@Resource(name = "DateUtil")
	private DateUtil dateutil;

	@Resource(name = "ZipUtil")
	private ZipUtil ziputil;
	
	public String excelUpdateDatas() throws Exception {//初审信息导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证号码");
			inputColumn.add("初审状态");
			inputColumn.add("初审备注");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
					AnLog log = new AnLog("系统日志",((AnAdmin)session.getAttribute("admin")).getAccount()+"于"+dateutil.getCurrentDate5()+" 上传初审"+path+"结果"+result, dateutil.getCurrentDate5());
					logManager.addData(log);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过5000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}
	
	public String excelUpdateDatas0() throws Exception {//标签导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证号码");
			inputColumn.add("标签");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
					AnLog log = new AnLog("系统日志",((AnAdmin)session.getAttribute("admin")).getAccount()+"于"+dateutil.getCurrentDate5()+" 上传"+path+"标签"+result, dateutil.getCurrentDate5());
					logManager.addData(log);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过10000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}

	public String excelUpdateDatas1() throws Exception {//准考证信息导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		FileOutputStream fos = null;
		FileInputStream fis = null;
		String msg = "";
		String path = "";
		try {		
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证号码");
			inputColumn.add("准考证模板");
			inputColumn.add("准考证编号");
			inputColumn.add("准考证备注");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
					AnLog log = new AnLog("系统日志",((AnAdmin)session.getAttribute("admin")).getAccount()+"于"+dateutil.getCurrentDate5()+" 上传准考证"+path+"结果"+result, dateutil.getCurrentDate5());
					logManager.addData(log);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过5000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}
	
	public String excelUpdateDatas2() throws Exception {//考试成绩导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证号码");
			inputColumn.add("通过状态");
			inputColumn.add("笔试成绩");
			inputColumn.add("面试成绩");
			inputColumn.add("综合测试成绩");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
					AnLog log = new AnLog("系统日志",((AnAdmin)session.getAttribute("admin")).getAccount()+"于"+dateutil.getCurrentDate5()+" 上传最终成绩"+path+"结果"+result, dateutil.getCurrentDate5());
					logManager.addData(log);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过5000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}

	public String excelUpdateDatas3() throws Exception {//缴费记录导入（身份证号）
		System.out.println("开始导入");
		
		response.setContentType("text/html; charset=UTF-8");		
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证号码");
			inputColumn.add("缴费状态");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
					AnLog log = new AnLog("系统日志",((AnAdmin)session.getAttribute("admin")).getAccount()+"于"+dateutil.getCurrentDate5()+" 上传缴费状态"+path+"结果"+result, dateutil.getCurrentDate5());
					logManager.addData(log);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过10000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}
	
	public String excelUpdateDatas4() throws Exception {//缴费记录导入（报名号）
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("报名编号");
			inputColumn.add("缴费状态");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData2(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
					AnLog log = new AnLog("系统日志",((AnAdmin)session.getAttribute("admin")).getAccount()+"于"+dateutil.getCurrentDate5()+" 上传缴费状态"+path+"结果"+result, dateutil.getCurrentDate5());
					logManager.addData(log);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过10000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}

	public String excelUpdateDatas5() throws Exception {//签收记录导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证号码");
			inputColumn.add("签收状态");
			
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					usList = eManager.ExcelData(path, inputColumn);
					String result = eManager.update(usList);
					dataMap.put("msg", result);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过10000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}

	public String excelUpdateDatas6() throws Exception {//导入教育厅的会考成绩
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsConfirm> cList = new ArrayList<ZsConfirm>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}		
		try {
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("姓名");
			inputColumn.add("身份证");
			inputColumn.add("语文");
			inputColumn.add("数学");
			inputColumn.add("英语");
			inputColumn.add("政治");
			inputColumn.add("历史");
			inputColumn.add("地理");
			inputColumn.add("物理");
			inputColumn.add("化学");
			inputColumn.add("生物");
			inputColumn.add("技术");
			inputColumn.add("品德表现");
			inputColumn.add("运动健康");
			inputColumn.add("艺术素养");
			inputColumn.add("创新实践");
			System.out.println("******************************教育厅确认历史数据"+inputColumn.size());
			dataMap.put("filename", getFileFileName());
			int count=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					cList = eManager.ExcelData3(path, inputColumn);
					//System.out.println("******************************清空教育厅确认历史数据");
					//List all = cManager.getDatas();
					//cManager.deleteDatas(all);
					String result = eManager.updateConfirm(cList);
					dataMap.put("msg", result);
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}			

			} else {
				msg = "excel 没有数据或者数据量超过10000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}

	public String excelUpdateDatas7() throws Exception {//教育厅报考学生导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		List<ZsUserStatus> usList = new ArrayList<ZsUserStatus>();
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		
		try {
			path = getSavePath() + "\\" + getFileFileName();
			System.out.println("path:"+path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		
		try {
			//List<String> inputColumn = Arrays.asList(selectCol.substring(2, selectCol.length()-2).split("\",\""));
			List<String> inputColumn = new ArrayList();
			inputColumn.add("BMXH");
			inputColumn.add("XM");
			inputColumn.add("BYXXMC");
			inputColumn.add("BYLB");
			inputColumn.add("XB");
			inputColumn.add("MZ");
			inputColumn.add("KSLB");
			inputColumn.add("ZZMM");
			inputColumn.add("ZJLX");
			inputColumn.add("SFZH");
			inputColumn.add("HJMC");
			inputColumn.add("LXDH");
			inputColumn.add("LXDH2");
			inputColumn.add("KL");
			inputColumn.add("WYYZ");
			inputColumn.add("SDMC");
			inputColumn.add("SXMC");
			inputColumn.add("BMDMC");
			inputColumn.add("BRTC");
			inputColumn.add("BZ");
			inputColumn.add("ZSQK");
			inputColumn.add("HJQK");
			inputColumn.add("YXDH");
			inputColumn.add("XKKM");
			inputColumn.add("XXBZ1");
			inputColumn.add("XXBZ2");
			inputColumn.add("XXBZ3");
			inputColumn.add("ZYDM1");
			inputColumn.add("ZYMC1");
			inputColumn.add("ZYDM2");
			inputColumn.add("ZYMC2");
			inputColumn.add("ZYDM3");
			inputColumn.add("ZYMC3");
			inputColumn.add("ZYDM4");
			inputColumn.add("ZYMC4");
			inputColumn.add("ZYDM5");
			inputColumn.add("ZYMC5");
			inputColumn.add("ZYDM6");
			inputColumn.add("ZYMC6");
			inputColumn.add("ZYDM7");
			inputColumn.add("ZYMC7");
			inputColumn.add("ZYDM8");
			inputColumn.add("ZYMC8");
			inputColumn.add("ZYDM9");
			inputColumn.add("ZYMC9");
			inputColumn.add("ZYDM10");
			inputColumn.add("ZYMC10");
			inputColumn.add("ZYFC");
			inputColumn.add("YWDD");
			inputColumn.add("SXDD");
			inputColumn.add("WYDD");
			inputColumn.add("ZZDD");
			inputColumn.add("LSDD");
			inputColumn.add("DLDD");
			inputColumn.add("WLDD");
			inputColumn.add("HXDD");
			inputColumn.add("SWDD");
			inputColumn.add("JSDD");
			inputColumn.add("XXDD");
			inputColumn.add("TYDD");
			inputColumn.add("YKXKKM");
			inputColumn.add("PDBX");
			inputColumn.add("YDJK");
			inputColumn.add("YSSY");
			inputColumn.add("CXSJ");

			
			dataMap.put("filename", getFileFileName());
			int count1=0;
			int count2=0;
			if (eManager.isExcelData(path)) {
				if ((msg=eManager.eqExcleColumn(path, inputColumn))=="") {	
					
					Workbook wb = ExcelRead(path);
					Row row = null;
					Sheet sheet = wb.getSheetAt(0);
					int rowNum = sheet.getLastRowNum();
					row = sheet.getRow(0);
					int colNum = row.getPhysicalNumberOfCells();
					for (int i = 1; i <= rowNum; i++) {
						ZsUser obj = new ZsUser();
						row = sheet.getRow(i);
						try {							
							
							obj.setUsIdcard(eManager.getCellFormatValue(row.getCell(9)).toString().trim());
							
							PageModule pg = manager.getDataByInstance(1, 1, null, null, obj);
							if(pg.getObjects().size()>0) {//如果数据库中已经存在同个身份证的考生信息，则导入信息会覆盖该考生信息
								obj=(ZsUser)pg.getObjects().get(0);
								obj.setUsBmxh(eManager.getCellFormatValue(row.getCell(0)).toString().trim());
								obj.setUsName(eManager.getCellFormatValue(row.getCell(1)).toString().trim());
								obj.setUsMobile(eManager.getCellFormatValue(row.getCell(11)).toString().trim());								
								obj.setAddTime(dateutil.getCurrentDate5());
								
								//obj.setEditTime(dateutil.getCurrentDate5());
								//obj.setLoginTime(dateutil.getCurrentDate5());
								obj.setUsActive(Status.USER_ACTIVE_OFF.getStatus());
								
								ZsUserinfo userinfo = imanager.getDataByUser(obj);
								
								String psw = userinfo.getInYwdd().toUpperCase()+userinfo.getInSxdd().toLowerCase()+userinfo.getInWydd().toLowerCase()+"#"+obj.getUsIdcard().substring(obj.getUsIdcard().length() - 6);
								obj.setUsPassword(md5.toMD5(psw));
								manager.updateData(obj);//给考生添加默认密码	
								
								userinfo = this.setUserinfo(userinfo, row);
								imanager.updateData(userinfo);
								count1++;
								AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 导入更新成功", dateutil.getCurrentDate5());
								logManager.addData(log);
								System.out.println("!!!更新第"+i+"条数据"+",身份证"+obj.getUsIdcard()+" 姓名："+obj.getUsName());
							} else {//否则就新增考生信息，并新增顺序号，并新增考生报考状态表
								obj.setUsId(idgenerate.nextId());
								
								//AnConfig config = anConfigManager.getConfigInfoByName("报名序号");//获取当前的报名序号
								AnAcquisitionHistory anac = new AnAcquisitionHistory();
								anac.setAcId(obj.getUsId());
								anac.setChannelUrl("报名序号编号");
								anac.setDescription("注册");
								anac.setContentUrl(obj.getUsIdcard());
								anac.setNeId(idgenerate.randomInt(100000000, 200000000));//写入id，使得neid唯一，确保抽签不重复写入
								acmanager.saveHistory(anac);
								int place = acmanager.getDataPlaceById(anac);					
								obj.setUsNo(anConfigManager.getSingleConfigInfoByName("报名号前缀") + smanager.getStringNo(place));
								obj.setUsBmxh(eManager.getCellFormatValue(row.getCell(0)).toString().trim());
								obj.setUsName(eManager.getCellFormatValue(row.getCell(1)).toString().trim());
								obj.setUsMobile(eManager.getCellFormatValue(row.getCell(11)).toString().trim());						
								obj.setAddTime(dateutil.getCurrentDate5());
								//obj.setEditTime(dateutil.getCurrentDate5());
								//obj.setLoginTime(dateutil.getCurrentDate5());
								obj.setUsActive(Status.USER_ACTIVE_OFF.getStatus());
								
								
								manager.addData(obj);
								ZsUserinfo userinfo = new ZsUserinfo();
								userinfo.setZsUser(obj);
								userinfo = this.setUserinfo(userinfo, row);
								imanager.addData(userinfo);
								
								String psw = userinfo.getInYwdd().toUpperCase()+userinfo.getInSxdd().toLowerCase()+userinfo.getInWydd().toLowerCase()+"#"+obj.getUsIdcard().substring(obj.getUsIdcard().length() - 6);
								obj.setUsPassword(md5.toMD5(psw));
								manager.updateData(obj);//给考生添加默认密码
								
								ZsSchool school = (ZsSchool)scmanager.getDatas().get(0);
								ZsUserStatus status = new ZsUserStatus();
								status.setUssId(idgenerate.nextId());
								status.setZsUser(obj);
								status.setScId(school.getScId());
								status.setScCode(school.getScCode());
								status.setUsIdcard(obj.getUsIdcard());
								status.setYear(Integer.parseInt(dateutil.getCurrentDate().substring(0, 3)));
								status.setAppStatus(Status.APPSTATUS_NULL.getStatus());
								status.setAppTime(dateutil.getCurrentDate5());
								status.setSendStatus(Status.SENDSTATUS_YES.getStatus());
								status.setFileStatus(Status.FILESTATUS_NULL.getStatus());
								status.setCheckStatus(Status.CHECKSTATUS_NULL.getStatus());
								status.setEmsStatus(Status.EMSSTATUS_NULL.getStatus());
								status.setPayStatus(Status.PAYSTATUS_NULL.getStatus());
								status.setPassStatus(Status.PASSSTATUS_NULL.getStatus());
								status.setLocalStatus(Status.LOCALSTATUS_NO.getStatus());			
								smanager.addData(status);
								count2++;
								AnLog log = new AnLog(obj.getUsId(),"考生日志",obj.getUsName()+"("+logManager.subIdcard(obj.getUsIdcard())+")"+"于"+dateutil.getCurrentDate5()+" 导入新增成功", dateutil.getCurrentDate5());
								logManager.addData(log);
								System.out.println("!!!新增第"+i+"条数据"+",身份证"+obj.getUsIdcard()+" 姓名："+obj.getUsName());
							}
							
						} catch (Exception e) {
							e.printStackTrace();
							//不存在，略过
						}
					}					
					dataMap.put("msg", "更新"+count1+"条学生信息，新增"+count2+"条学生信息");
				} else {
					msg+="上述字段不符合要求";
					System.out.println(msg);
					dataMap.put("flag", "error");
					dataMap.put("msg", msg);
				}
			} else {
				msg = "excel 没有数据或者数据量超过50000";
				System.out.println(msg);
				dataMap.put("flag", "error");
				dataMap.put("msg", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "excel 字段格式有误，系统无法转换";
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
			return ERROR;
		}		
		return SUCCESS;
	}
	
	private ZsUserinfo setUserinfo(ZsUserinfo userinfo, Row row) {
		userinfo.setInByxxmc(eManager.getCellFormatValue(row.getCell(2)).toString().trim());
		userinfo.setInBylb(eManager.getCellFormatValue(row.getCell(3)).toString().trim());
		userinfo.setInXb(eManager.getCellFormatValue(row.getCell(4)).toString().trim());
		userinfo.setInMz(eManager.getCellFormatValue(row.getCell(5)).toString().trim());
		userinfo.setInKslb(eManager.getCellFormatValue(row.getCell(6)).toString().trim());
		userinfo.setInZzmm(eManager.getCellFormatValue(row.getCell(7)).toString().trim());
		userinfo.setInZjlx(eManager.getCellFormatValue(row.getCell(8)).toString().trim());
		userinfo.setInHjmc(eManager.getCellFormatValue(row.getCell(10)).toString().trim());
		userinfo.setInLxdh(eManager.getCellFormatValue(row.getCell(11)).toString().trim());
		userinfo.setInLxdh2(eManager.getCellFormatValue(row.getCell(12)).toString().trim());
		userinfo.setInKl(eManager.getCellFormatValue(row.getCell(13)).toString().trim());
		userinfo.setInWyyz(eManager.getCellFormatValue(row.getCell(14)).toString().trim());
		userinfo.setInSdmc(eManager.getCellFormatValue(row.getCell(15)).toString().trim());
		userinfo.setInSxmc(eManager.getCellFormatValue(row.getCell(16)).toString().trim());
		userinfo.setInBmdmc(eManager.getCellFormatValue(row.getCell(17)).toString().trim());
		userinfo.setInBrtc(eManager.getCellFormatValue(row.getCell(18)).toString().trim());
		userinfo.setInBz(eManager.getCellFormatValue(row.getCell(19)).toString().trim());
		userinfo.setInZsqk(eManager.getCellFormatValue(row.getCell(20)).toString().trim());
		userinfo.setInHjqk(eManager.getCellFormatValue(row.getCell(21)).toString().trim());
		userinfo.setInYxdh(eManager.getCellFormatValue(row.getCell(22)).toString().trim());
		userinfo.setInXkkm(eManager.getCellFormatValue(row.getCell(23)).toString().trim());
		userinfo.setInXxbz1(eManager.getCellFormatValue(row.getCell(24)).toString().trim());
		userinfo.setInXxbz2(eManager.getCellFormatValue(row.getCell(25)).toString().trim());
		userinfo.setInXxbz3(eManager.getCellFormatValue(row.getCell(26)).toString().trim());
		userinfo.setInZydm01(eManager.getCellFormatValue(row.getCell(27)).toString().trim());
		userinfo.setInZymc01(eManager.getCellFormatValue(row.getCell(28)).toString().trim());
		userinfo.setInZydm02(eManager.getCellFormatValue(row.getCell(29)).toString().trim());
		userinfo.setInZymc02(eManager.getCellFormatValue(row.getCell(30)).toString().trim());
		userinfo.setInZydm03(eManager.getCellFormatValue(row.getCell(31)).toString().trim());
		userinfo.setInZymc03(eManager.getCellFormatValue(row.getCell(32)).toString().trim());
		userinfo.setInZydm04(eManager.getCellFormatValue(row.getCell(33)).toString().trim());
		userinfo.setInZymc04(eManager.getCellFormatValue(row.getCell(34)).toString().trim());
		userinfo.setInZydm05(eManager.getCellFormatValue(row.getCell(35)).toString().trim());
		userinfo.setInZymc05(eManager.getCellFormatValue(row.getCell(36)).toString().trim());
		userinfo.setInZydm06(eManager.getCellFormatValue(row.getCell(37)).toString().trim());
		userinfo.setInZymc06(eManager.getCellFormatValue(row.getCell(38)).toString().trim());
		userinfo.setInZydm07(eManager.getCellFormatValue(row.getCell(39)).toString().trim());
		userinfo.setInZymc07(eManager.getCellFormatValue(row.getCell(40)).toString().trim());
		userinfo.setInZydm08(eManager.getCellFormatValue(row.getCell(41)).toString().trim());
		userinfo.setInZymc08(eManager.getCellFormatValue(row.getCell(42)).toString().trim());
		userinfo.setInZydm09(eManager.getCellFormatValue(row.getCell(43)).toString().trim());
		userinfo.setInZymc09(eManager.getCellFormatValue(row.getCell(44)).toString().trim());
		userinfo.setInZydm10(eManager.getCellFormatValue(row.getCell(45)).toString().trim());
		userinfo.setInZymc10(eManager.getCellFormatValue(row.getCell(46)).toString().trim());
		userinfo.setInZyfc(eManager.getCellFormatValue(row.getCell(47)).toString().trim());
		userinfo.setInYwdd(eManager.getCellFormatValue(row.getCell(48)).toString().trim());
		userinfo.setInSxdd(eManager.getCellFormatValue(row.getCell(49)).toString().trim());
		userinfo.setInWydd(eManager.getCellFormatValue(row.getCell(50)).toString().trim());
		userinfo.setInZzdd(eManager.getCellFormatValue(row.getCell(51)).toString().trim());
		userinfo.setInLsdd(eManager.getCellFormatValue(row.getCell(52)).toString().trim());
		userinfo.setInDldd(eManager.getCellFormatValue(row.getCell(53)).toString().trim());
		userinfo.setInWldd(eManager.getCellFormatValue(row.getCell(54)).toString().trim());
		userinfo.setInHxdd(eManager.getCellFormatValue(row.getCell(55)).toString().trim());
		userinfo.setInSwdd(eManager.getCellFormatValue(row.getCell(56)).toString().trim());
		userinfo.setInJsdd(eManager.getCellFormatValue(row.getCell(57)).toString().trim());
		userinfo.setInXxdd(eManager.getCellFormatValue(row.getCell(58)).toString().trim());
		userinfo.setInTydd(eManager.getCellFormatValue(row.getCell(59)).toString().trim());
		userinfo.setInYkxkkm(eManager.getCellFormatValue(row.getCell(60)).toString().trim());
		userinfo.setInPdbx(eManager.getCellFormatValue(row.getCell(61)).toString().trim());
		userinfo.setInYdjk(eManager.getCellFormatValue(row.getCell(62)).toString().trim());
		userinfo.setInYssy(eManager.getCellFormatValue(row.getCell(63)).toString().trim());
		userinfo.setInCxsj(eManager.getCellFormatValue(row.getCell(64)).toString().trim());
		return userinfo;
	}
	
	public String studentKSZPZipUpdateDatas() throws Exception {// 学生照片压缩包导入
		response.setContentType("text/html; charset=UTF-8");
		System.out.println("开始导入");
		dataMap = new HashMap<String, Object>();
		String msg = "";
		String path = "";
		String unzippath = anConfigManager.getSingleConfigInfoByName("网站目录") + "\\photo\\";
		FileOutputStream fos = null;
		FileInputStream fis = null;
		File[] files = null;
		File[] zpfiles = null;
		try {
			path = getSavePath() + "\\" + getFileFileName();
			String dt = dateutil.getCurrentDate6();
			//unzippath = unzippath + dt + "//";
			System.out.println("path:" + path);
			// 建立文件输出流
			fos = new FileOutputStream(path);
			// 建立文件上传流
			fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			System.out.println("path:" + path+" unzippath:" +  unzippath);
			files = ziputil.unzip(path, unzippath, null);
			

			msg = "上传成功" + unzippath;
			System.out.println(msg);
			dataMap.put("flag", "error");
			dataMap.put("msg", msg);
		} catch (Exception e) {
			close(fos, fis);
			System.out.println("文件上传失败");
			dataMap.put("flag", "error");
			dataMap.put("msg", "文件上传失败！请确认压缩包内没有多层文件夹");
			e.printStackTrace();
			return ERROR;
		} finally {
			close(fos, fis);
		}
		return SUCCESS;

	}
	
	private void close(FileOutputStream fos, FileInputStream fis) {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				System.out.println("FileInputStream关闭失败");
				e.printStackTrace();
			}
		}
		if (fos != null) {
			try {
				fos.close();
			} catch (IOException e) {
				System.out.println("FileOutputStream关闭失败");
				e.printStackTrace();
			}
		}
	}

	public Workbook ExcelRead(String path) throws Exception {
		Workbook wb = null;
		File f = new File(path);
		FileInputStream is = new FileInputStream(f);
		String fileName = f.getName();
		String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
		if ("XLS".equals(String.valueOf(prefix).toUpperCase())) {// 2003
			wb = new HSSFWorkbook(is);
		} else if ("XLSX".equals(String.valueOf(prefix).toUpperCase())) {// 2007
			wb = new XSSFWorkbook(is);
		}
		return wb;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getSelectCol() {
		return selectCol;
	}

	public void setSelectCol(String selectCol) {
		this.selectCol = selectCol;
	}

	public ZsExcelManager geteManager() {
		return eManager;
	}

	public void seteManager(ZsExcelManager eManager) {
		this.eManager = eManager;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}
}
